<%
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") {
  xml.Document {

    
    # MEASURE LAYER
    xml.Folder{
      xml.name("traces")
      xml.open(0)
      xml.visible(1)
      @tracks.each {|t|
        xml.Folder{
        xml.name("trace #{t.id}")
        t.measures.find(:all, :conditions=>"corrected is not null").each_with_index { |point, i|
          
            xml.Placemark{
              xml.name("#{point.loudness} dB(A)")
              # #set the color according the loudness
              xml.Style{  
                xml.PolyStyle{
                  color=get_color(point)
                  #  color="af"+point.color[1..6]
                  # #color=point.color[1..6]
                  xml.color(color)
                  xml.colorMode("normal")
                  xml.fill(true)
                  xml.outline(false)
                }
              }
              xml.TimeStamp{
                xml.when("#{point.made_at.localtime.strftime("%Y-%m-%dT%H:%M:%S+00:00")}") #WHY? localtime
                }
              coord=generate_kmlcircle(3100+point.loudness,point.corrected.lng,point.corrected.lat,10,6)

              xml.MultiGeometry{
                #    xml.Point{
                #      xml.coordinates("#{point.lng},#{point.lat},#{point.loudness}")
                #    }
                xml.Polygon{
                  xml.extrude(true)
                  # #xml.tessellate(true) #xml.altitudeMode("clampToGround")
                  xml.outerBoundaryIs{
                    xml.LinearRing{
                      xml.coordinates(coord)  
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
      }
    }

          ## SUBJECTIVITY
          ##      xml.Folder {
          ##        xml.name("Impression")
          ##        measure.each { |measure|
          ##         if measure.rating.nil?
          ##          xml.Placemark{
          ##            xml.name("rating tag.name)
          ## 
          ## 
          ##            #set the color according the loudness
          ##            xml.Style{
          ##              xml.PolyStyle{
          ##                color="F000FF00" if (tag.feeling==2)
          ##                color="F0808000" if (tag.feeling==1)
          ##                color="F0a0a0a0" if (tag.feeling==0)
          ##                color="F005A0DD" if (tag.feeling==-1)
          ##                color="F00000FF" if (tag.feeling==-2)
          ##                puts color
          ##                xml.color(color)
          ##                xml.colorMode("normal")
          ##                xml.fill(true)
          ##                xml.outline(false)
          ##              }
          ##            }
          ## 
          ##            coord=create_circle(tag.lng,tag.lat,10)
          ## 
          ##            xml.Polygon{
          ##              xml.extrude(true)
          ##              xml.tessellate(true)
          ##              xml.altitude("clampToGround")
          ##              xml.outerBoundaryIs{
          ##                xml.LinearRing{
          ##                  xml.coordinates(coord)
          ##                }
          ##              }
          ##            }
          ##          }
          ##        }
          ##      }
     
    %><%= xml.target! %>